package 
{
import com.duobiduo.audio.AudioChannelX;
import com.duobiduo.audio.filters.TDSI.PitchFilter;
import com.duobiduo.audio.stream.AudioXStream;

import flash.display.Sprite;
import flash.events.ActivityEvent;
import flash.events.Event;

import org.as3wavsound.WavSound;
import org.bytearray.micrecorder.MicRecorder;
import org.bytearray.micrecorder.encoder.WaveEncoder;
import org.bytearray.micrecorder.events.RecordingEvent;

/**
 * ...
 * 
 */
[SWF(width=320,height=240)]
public class Test extends Sprite 
{
    //==========================================================================
    //  Constructor
    //==========================================================================
    /**
     * Constructs a new <code>Test</code> instance.
     * 
     */    
    public function Test():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }
    //==========================================================================
    //  Variables
    //==========================================================================
    private var recorder:MicRecorder;
    private var audio:AudioXStream;
    private var chanel:AudioChannelX;
    private var length:Number = 0;
    private var pitchFilter:PitchFilter;
    private var soundPlayer:WavSound;
    //==========================================================================
    //  Methods
    //==========================================================================
    private function init(event:Event = null):void
    {
        recorder = new MicRecorder(new WaveEncoder(), null, 100, 44, 30, 1000);
        recorder.addEventListener(RecordingEvent.RECORDING, recordingHandler);
        recorder.addEventListener(Event.COMPLETE, recordCompleteHandler);
        recorder.record();
        recorder.microphone.addEventListener(ActivityEvent.ACTIVITY, microActivityHandler);
    }
    //==========================================================================
    //  Event Handlers
    //==========================================================================
    private function recordingHandler(event:RecordingEvent):void
    {
    }
    
    private function recordCompleteHandler(event:Event):void
    {
        //        soundPlayer = new WavSound(recorder.output);
        //        soundPlayer.play();
        //        recorder.record();
        //        trace(recorder.output);
        if (recorder.output)
        {
            trace("output");
            if (audio == null)
            {
                audio = new AudioXStream();
            }
            audio.addEventListener(Event.COMPLETE, initAudioInfo);
            trace("init");
            audio.loadBytes(recorder.output);
        }
        recorder.record();
    }
    
    private function microActivityHandler(event:ActivityEvent):void
    {
        trace(event.activating);
        if (event.activating)
        {
            //
        }
        else
        {
            recorder.stop();
        }
    }
    
    private function initAudioInfo(event:Event):void
    {
        chanel = audio.createChannel();
        length = chanel.length;
        pitchFilter = new PitchFilter();
        chanel.bufferFilters.push(pitchFilter);
        chanel.play();
        trace("play");
    }
}
}